import React, { Component, Fragment } from 'react'
import { Redirect, Route, Switch } from 'react-router-dom';
import memoryUtils from '../../utils/memoryUtils'
import { Layout } from 'antd';
import Header from '../../components/header';
import Leftnav from '../../components/left-nav'
import Home from '../home'
import Category from '../category'
import Bar from '../charts/bar'
import Line from '../charts/line'
import Pie from '../charts/pie'
import Product from '../product'
import Role from '../role'
import User from '../user'
import './index.less'
import menuList from '../../config/menuConfig'
const { Footer, Sider, Content } = Layout;
export default class index extends Component {
  getmenuList = (menuList) => {
    const pathname = this.props.location.pathname

    let title = ''
    if (pathname === '/product/addupdate' || pathname === '/product/detail') {
      return title = '商品管理'
    }
    menuList.forEach((list) => {
      if (list.key === pathname) {
        title = list.title
      } else if (list.children) {
        list.children.forEach(
          (c) => {
            if (c.key === pathname) {
              title = c.title
            }
          }
        )
      }
    })

    return title
  }
  render() {
    if (!memoryUtils.user || !memoryUtils.user._id) {
      return <Redirect to='/login' />
    }
    return (
      <Fragment>
        <Layout style={{ height: '100%' }}>
          <Sider>
            <Leftnav></Leftnav>
          </Sider>
          <Layout>
            <Header title={this.getmenuList(menuList)}>Header</Header>
            <Content className='content'>
              <Switch>
                <Route path='/home' component={Home}></Route>
                <Route path='/category' component={Category}></Route>
                <Route path='/product' component={Product}></Route>
                <Route path='/role' component={Role}></Route>
                <Route path='/user' component={User}></Route>
                <Route path='/charts/line' component={Line}></Route>
                <Route path='/charts/pie' component={Pie}></Route>
                <Route path='/charts/bar' component={Bar}></Route>
                <Redirect to='/home' />
              </Switch>
            </Content>
            <Footer style={{ textAlign: 'center' }}>本站由 <a href='https://react.docschina.org/'>React中文网</a>&nbsp;
              <a href='https://ant-design.gitee.io/index-cn'>Antd中文网</a>&nbsp;
              <a href='https://bizcharts.net/'>bizcharts中文网</a>&nbsp;
              <a href='https://echarts.apache.org/'>echarts中文网</a>&nbsp;
              <a href='http://nodejs.cn/'>Node.js中文网</a>  提供技术支持</Footer>
          </Layout>
        </Layout>
      </Fragment>
    )
  }
}
